<template>
  <div>
    <div class="boxa">
      <div class="boxb">
        <input type="text" placeholder="请输入城市名" v-model="seach" />
      </div>
    </div>
    <div class="searchList" ref="search" v-show="seach">
      <ul class="">
        <li class="search-item"  v-for="(item, index) in list" :key="index" @click="add(item.name)">
          {{ item.name }}
        </li>
        <li class="search-item" v-show="!list.length">没有数据奥</li>
      </ul>
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
export default {
  name: "",
  components: {},
  props: ["cities"],
  data() {
    return {
      seach: "",
      list: [],
    };
  },
  computed: {},
  watch: {
    seach() {
      let result = [];
      for (let i in this.cities) {
        // console.log(this.cities[i])
        this.cities[i].forEach((value) => {
          if (
            value.spell.indexOf(this.seach) > -1 ||
            value.name.indexOf(this.seach) > -1
          ) {
            result.push(value);
          }
        });
        this.list = result;
        // console.log(this.list);
      }
    },
  },
  created() {},
  mounted() {
    // 默认阻止了点击事件
    this.scroll = new Bscroll(this.$refs.search, { click: true });
    // 把地方传给vuex
  },
  methods: {
    // 把地方传给vuex
    add(txt) {
      // 传给vuex
      this.$store.commit("city", txt)
      this.$router.push("/")
    },
  },
};
</script>

<style lang = "stylus" scoped>
.boxa {
  display: block;
  height: 0.7rem;
  background: #00bcd4;
  width: 100%;

  .boxb {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto;
    height: 0.7rem;

    input {
      text-align: center;
      width: 80%;
      border-radius: 0.3rem;
    }
  }
}

.searchList {
  z-index: 1;
  overflow: hidden;
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;

  .search-item {
    line-height: 0.62rem;
    padding-left: 0.2rem;
    background: #fff;
    color: #666;
  }
}
</style>
